Learn With Alpha Logo

C Programming Language Notes 💻

Comprehensive guide for beginners and students

1. Introduction and Fundamentals

What is C?

Key Features:

Basic Program Structure:

#include <stdio.h> // Preprocessor directive

int main() {
    printf("Hello, World!\n");
    return 0;
}

2. Data Types and Variables

Data Type Description Size (bytes) Format Specifier
int Integer 2 or 4 %d or %i
float Real Number (single-precision) 4 %f
double Double-precision floating point 8 %lf
char Single character 1 %c

Variables: Declaration & Initialization

int age;
age = 30;
int roll = 101;

Constants:

#define PI 3.14159
const float pi = 3.14159;

3. Operators

4. Control Flow

Decision Making:

switch (expression) {
    case 1:
        // code
        break;
    default:
        // code
}

Loops: for, while, do-while

for(int i=0; i<5; i++) {
    printf("%d", i);
}

5. Functions

int add(int a, int b) {   // Definition
    return a + b;
}

int main() {
    int sum = add(5, 3);   // Call
    printf("%d", sum);
    return 0;
}

6. Arrays and Strings

int numbers[5] = {1,2,3,4,5};
printf("%d", numbers[0]);

char name[20] = "John Doe";
printf("%s", name);

7. Pointers

int x = 10;
int *ptr = &x;
printf("%d", *ptr);

8. Structures and Unions

struct Student {
    int id;
    char name[50];
    float gpa;
};

struct Student s1;
s1.id = 101;

9. File Handling

FILE *fp = fopen("data.txt", "w");
fprintf(fp, "Hello File!");
fclose(fp);